<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
  <div class="execution">
    <basic-container>
      <el-row style="margin-bottom: 20px; font-size: 15px">
        <el-col :span="6"
        >是否开启活动审核:
          <el-switch
            active-color="#13ce66"
            inactive-color="#ff4949"
            size="small"
            @change="swichChenge"
            :active-value="1"
            :inactive-value="2"
            v-model="showSwich.activityAudit"
          >
          </el-switch
          >
        </el-col>
      </el-row>
      <avue-crud
        ref="crud"
        v-model="form"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        :search.sync="searchForm"
        :upload-error="uploadError"
        @search-change="searchChange"
        @search-reset="searchReset"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @rowEdit="listenEdit"
        @row-save="handleSave"
        @row-del="rowDel"
      >
        <template slot-scope="scope" slot="menu" v-if="showSwich.activityAudit = 1">
          <el-button type="text" @click="through(scope)">通过</el-button>
          <el-button type="text" style="color:#f56c6c" @click="noThrough(scope)">不通过</el-button>
        </template>
        <template slot-scope="{ disabled, size }" slot="signTimeSearch">
          <el-date-picker
            v-model="filterTime"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>
<script>
import {
  fetchList,
  fetchList1,
  getObj,
  addObj,
  putObj,
  putObj1,
  delObj,
  noSignPeople,
  updateSignState,
  fetchListAudit
} from '@/api/circleactivity'
import {tableOption, tableOption1} from '@/const/crud/circleactivityaudit'
import {mapGetters} from 'vuex'
import AvueUeditor from 'avue-plugin-ueditor'

export default {
  name: 'circleactivity',
  data() {
    return {
      auditObj: {
        id: '',
        audit: ''
      },
      showSwich: {},
      filterTime: [],
      multipleSelection: [],
      signData: [],
      form: {},
      searchForm: {},
      tableData: [],
      tableData1: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      page1: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      tableLoading1: false,
      tableOption1: tableOption1,
      dialogPermissionVisible: false,
      signVisible: false
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.circleActivity_circleactivity_add, false),
        delBtn: this.vaildData(this.permissions.circleActivity_circleactivity_del, false),
        editBtn: this.vaildData(this.permissions.circleActivity_circleactivity_edit, false)
      };
    }
  },
  watch: {
    'form.type': {
      handler(val) {
        var isTourists = this.findObject(this.tableOption.column, 'isTourists')
        if (val === 1) {
          isTourists.display = true
        }
        if (val === 2) {
          isTourists.display = false
        }
      }
    },
    'form.isNumberLimit': {
      handler(val) {
        var peopleNumObj = this.findObject(this.tableOption.column, 'peopleNum')
        var outsidersNum = this.findObject(this.tableOption.column, 'outsidersNum')
        if (val === 1) {
          //开启
          peopleNumObj.display = true
          peopleNumObj.rules = [{
            required: true,
            message: "请选择是否限制",
            trigger: "blur"
          }, {
            validator: (rule, value, callback) => {
              if (value < 1) {
                callback(new Error('会员限制人数必须大于0'));
              } else {
                callback();
              }
            }, trigger: 'blur'
          }]
          outsidersNum.display = true
          outsidersNum.rules = [{
            required: true,
            message: "请选择是否限制",
            trigger: "blur"
          }, {
            validator: (rule, value, callback) => {
              if (value < 1) {
                callback(new Error('游客限制人数必须大于0'));
              } else {
                callback();
              }
            }, trigger: 'blur'
          }]
        } else {
          //关闭
          peopleNumObj.display = false;
          peopleNumObj.rules = [];
          outsidersNum.display = false;
          outsidersNum.rules = [];
        }
      },
      immediate: true
    },
  },
  mounted() {
    var vm = this
    axios.get('/auditConfiguration/auditConfiguration').then(r => {

      if (r.data.data != null) {
        vm.showSwich = r.data.data;

      }

    })

  },
  methods: {

    swichChenge(e) {
      var vm = this
      console.log(e)
      if (e == 1) {
        axios.post('/auditConfiguration/updateAudit', vm.showSwich).then(r => {
          vm.getList(vm.page)
        })
      }
      if (e == 2) {
        console.log('222', vm.showSwich.activityAudit)
        axios.post('/auditConfiguration/updateAudit', vm.showSwich).then(r => {
          vm.getList(vm.page)
        })
      }
    },


    through(data) {
      console.log(data.row.id);
      let vm = this;
      vm.auditObj.id = data.row.id
      this.$confirm("是否通过审核", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        data.row.audit = 1
        vm.auditObj.audit = 1
        putObj1(vm.auditObj)
          .then(r => {
            vm.$message.success("审核成功");
            vm.getList(vm.page);
          })
          .catch(() => {
          });
      })
    },
    noThrough(data) {
      console.log(data.row.id);
      let vm = this;
      vm.auditObj.id = data.row.id
      this.$confirm("是否不通过审核", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        data.row.audit = 2
        vm.auditObj.audit = 2
        putObj1(vm.auditObj)
          .then(r => {
            vm.$message.success("审核成功");
            vm.getList(vm.page);
          })
          .catch((r) => {
            console.log(r);
          });
      })
    },
    listenEdit(row, index) {
      var peopleNumObj = this.findObject(this.tableOption.column, 'peopleNum')
      var outsidersNum = this.findObject(this.tableOption.column, 'outsidersNum')
      if (row.isNumberLimit === 1) {
        //开启
        peopleNumObj.display = true
        peopleNumObj.rules = [{
          required: true,
          message: "请选择是否限制",
          trigger: "blur"
        }, {
          validator: (rule, value, callback) => {
            if (value < 1) {
              callback(new Error('会员限制人数必须大于0'));
            } else {
              callback();
            }
          }, trigger: 'blur'
        }]
        outsidersNum.display = true
        outsidersNum.rules = [{
          required: true,
          message: "请选择是否限制",
          trigger: "blur"
        }, {
          validator: (rule, value, callback) => {
            if (value < 1) {
              callback(new Error('游客限制人数必须大于0'));
            } else {
              callback();
            }
          }, trigger: 'blur'
        }]
      } else {
        //关闭
        peopleNumObj.display = false;
        peopleNumObj.rules = [];
        outsidersNum.display = false;
        outsidersNum.rules = [];
      }
    },
    getList(page, params) {
      this.tableLoading = true
      fetchListAudit(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        var records = response.data.data.records;
        //数据转换
        records.map(r => {
          if (r.activityStartTime && r.activityEndTime) {
            r.activityTime = [r.activityStartTime, r.activityEndTime];
          }
          if (r.signStartTime && r.signEndTime) {
            r.signTime = [r.signStartTime, r.signEndTime];
          }
          if (r.positioning) {
            var jwArr = r.positioning.split(',');
            jwArr.push(r.address);
            r.address = jwArr;
          }
          if (r.label) {
            r.label = r.label.split(',');
          }
          return r;
        })

        this.tableData = records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    rowDel: function (row, index) {
      this.$confirm('是否确认删除活动名为：' + row.totle, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    handleUpdate: function (row, index, done, loading) {
      //数据转换
      row.positioning = row.address[0] + ',' + row.address[1];
      row.address = row.address[2];
      if (row.label) {
        row.label = row.label.join(',');
      }
      if (row.isNumberLimit === 2) {
        row.peopleNum = 0;
      }
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    handleSave: function (row, done, loading) {
      //数据转换
      row.positioning = row.address[0] + ',' + row.address[1];
      row.address = row.address[2];
      // row.label = row.label.join(',');
      // if (row.isNumberLimit === 2) {
      //   row.peopleNum = 0;
      // }
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    currentChange(current) {
      this.page.currentPage = current
    },
    searchChange(form, done) {
      var filterTime = this.filterTime;
      if (filterTime != null) {
        if (filterTime.length > 0) {
          this.searchForm.signStartTime = filterTime[0];
          this.searchForm.signEndTime = filterTime[1];
        }
      }

      delete this.searchForm.signTime;
      this.searchForm = {...this.searchForm, ...form}
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    searchReset() {
      this.searchForm = {};
      this.filterTime = [];
      this.refreshChange();
    },
    refreshChange() {
      this.getList(this.page)
    },
    excel(row) {
      window.open(baseUrl + "/circleactivity/exportExcel?activityId=" + row.id);

    },
    getList1(page, params) {
      this.tableLoading1 = true
      var params = params ? params : {}
      params.dynamic = this.rowid
      fetchList1(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm1)).then(response => {
        this.tableData1 = response.data.data.records
        this.page1.total = response.data.data.total
        this.tableLoading1 = false
      }).catch(() => {
        this.tableLoading1 = false
      })
    },
    goscoreset(row) {
      this.dialogPermissionVisible = true
      var dic = {id: row.id}
      this.getList1(this.page1, dic)
    },

    //
    toggleSelection(rows) {
      console.log(this.multipleSelection);
      updateSignState(this.multipleSelection).then(res => {
        this.$message.success('修改成功')
        this.signVisible = false
      }).catch(() => {
        this.$message.success('修改失败')
      })


    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    sign(row) {
      this.signVisible = true;
      let id = row.id
      this.getSignData(id)
    },


    getSignData(obj) {
      noSignPeople(obj).then(res => {
        this.signData = res.data.data.records
      })
    },

    uploadError(error, column) {
      this.$message.error('图片过大，上传失败')
    }


  }


}
</script>
